<template>
  <el-dialog v-el-drag-dialog title="编辑流程模型" :visible.sync="visible" :append-to-body="appendToBody" :before-close="onClose">
    <el-form ref="modelForm" :model="form" :rules="rules" label-width="120px" :disabled="!hasLoaded">
      <el-card>
        <el-form-item label="模型标识" prop="modelKey">
          <el-input v-model="form.modelKey" clearable :disabled="form.modelId !== undefined" />
        </el-form-item>
        <el-form-item label="模型名称" prop="modelName">
          <el-input v-model="form.modelName" clearable />
        </el-form-item>
        <el-form-item label="流程分类" prop="category">
          <el-select v-model="form.category">
            <el-option
              v-for="item in optionOfCategory"
              :key="item.dictCode"
              :label="item.dictLabel"
              :value="item.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" type="textarea" maxlength="200" show-word-limit />
        </el-form-item>
      </el-card>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button v-if="hasLoaded" type="primary" icon="el-icon-check" @click="onSubmit">确 定</el-button>
      <el-button icon="el-icon-close" @click="onClose">关 闭</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { categoryGroup } from '@/api/tool/workflow'

export default {
  name: 'ModelDialog',
  data() {
    return {
      visible: false,
      appendToBody: false,
      hasLoaded: false,
      form: {
        modelId: undefined,
        modelKey: undefined,
        modelName: undefined,
        category: undefined,
        description: undefined
      },
      rules: {
        modelKey: [
          { required: true, message: '模型标识不能为空' }
        ],
        modelName: [
          { required: true, message: '模型名称不能为空' }
        ],
        category: [
          { required: true, message: '请选择类型', trigger: 'change' }
        ]
      },
      optionOfCategory: []
    }
  },
  methods: {
    init(data) {
      this.hasLoaded = false
      this.visible = true
      this.$nextTick(() => {
        this.loadForm(data)
        this.getDictData()
      })
    },
    loadForm(data) {
      if (data) {
        this.form = this.$api.objectUpdate(this.form, data)
      } else {
        const dateTime = new Date().getTime()
        this.form.modelId = undefined
        this.form.modelKey = `Process_${dateTime}`
        this.form.modelName = `业务流程_${dateTime}`
      }

      // 判断当前用户权限
      this.hasLoaded = this.$perm.hasPermissionOr(['flowable:model:edit'])
    },
    getDictData() {
      categoryGroup().then(response => {
        this.optionOfCategory = response
      }).catch(e => {})
    },
    onSubmit() {
      this.$refs.modelForm.validate(valid => {
        if (valid) {
          let url = `${process.env.VUE_APP_MODULE_ADMIN_API}/api/workflow/model/insert`
          if (this.form.modelId !== undefined) {
            url = `${process.env.VUE_APP_MODULE_ADMIN_API}/api/workflow/model/update`
          }
          this.$http.post(url, this.form).then(response => {
            this.$modal.msgSuccess(response.msg)
            this.$emit('confirmForm')
            this.onClose()
          }).catch(() => {})
        }
      })
    },
    onClose() {
      this.$refs.modelForm.resetFields()
      this.visible = false
    }
  }
}
</script>
